package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.controller.info.MenusInfo;
import com.woniuxy.dao.VoteMenusDao;
import com.woniuxy.dao.VoteRoleMenusDao;
import com.woniuxy.model.VoteMenus;
import com.woniuxy.service.VoteMenusService;
import com.woniuxy.util.Result;
import com.woniuxy.util.WoniuxyBeanCopyUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhouzhou
 * @since 2022-06-23
 */
@Service
public class VoteMenusServiceImpl extends ServiceImpl<VoteMenusDao, VoteMenus> implements VoteMenusService {

    @Resource
    VoteMenusService voteMenusService;

    @Resource
    VoteRoleMenusDao voteRoleMenusDao;

    @Override
    public Result getMenuesByAccountId(Integer id) {
        List<VoteMenus> menus = voteRoleMenusDao.findVoteListByAccountId(id);
        List<MenusInfo> menusInfos = WoniuxyBeanCopyUtil.copyList(menus, MenusInfo.class);
        ArrayList<MenusInfo> arlist = new ArrayList<>();
        //去遍历menusInfos，如果他的属性MenusParentid=0，证明他是父节点
        for (MenusInfo menusInfo : menusInfos) {
            if (menusInfo.getMenusParentid()==0) {
                arlist.add(menusInfo);
                for (MenusInfo menusInfo2 : menusInfos) {
                    if (menusInfo2.getMenusParentid()==menusInfo.getMenusId()) {
                        if(menusInfo.getChildren()==null) menusInfo.setChildren(new ArrayList<MenusInfo>());
                        menusInfo.getChildren().add(menusInfo2);
                    }
                }
            }
        }
        return Result.success(200,"查询成功",arlist);
    }
}
